package net.monare.mifss.admin.ui.mod;

import net.monare.mifss.MifssMessages;
import net.monare.mifss.admin.ui.valid.ApiKeyModeValidator;
import net.monare.mifss.admin.ui.valid.EmptyStringValidator;
import net.monare.mifss.configurations.pojo.ApiKey;
import net.monare.mifss.configurations.pojo.ApiMode;

import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;

public class ApiKeyUiModule extends FormLayout {

	private static final long serialVersionUID = 6468654027801440553L;

	private TextField name;
	private ComboBox apiMode;
	private TextField description;
	private TextField hash;

	public ApiKeyUiModule() {

		initElements();

	}

	public ApiKeyUiModule(ApiKey apiKey) {

		initElements();
		populateElements(apiKey);

	}

	public void setApiKey(ApiKey apiKey) {

		populateElements(apiKey);

	}
	
	public void protectFields(){
		hash.setEnabled(false);
        name.setEnabled(false);
	}
	
	
	public ApiKey getApiKey() {

		ApiKey apiKey = new ApiKey();

		if (name.isValid()) {
			apiKey.setName(name.getValue());
		} else {
			return null;
		}

		if (apiMode.isValid()) {
			apiKey.setApiMode((ApiMode) apiMode.getValue());
		} else {
			return null;
		}

		if (description.isValid()) {
			apiKey.setDescription(description.getValue());
		} else {
			return null;
		}

		if (hash != null) {
			apiKey.setHash(hash.getValue());
		}

		return apiKey;

	}

	public void addFormElement(Component component) {
		addComponent(component);
	}

	private void initElements() {

		// load UI text
		String apiNameLabel = MifssMessages.getMessage("label.api.name");
		String apiModeLabel = MifssMessages.getMessage("label.api.mode");
		String apiDescriptionLabel = MifssMessages.getMessage("label.api.description");
		String stringValidationMsg = MifssMessages.getMessage("validation.empty.string");
		String apiModeValidationMsg = MifssMessages.getMessage("validation.select.api.mode");

		// initialize UI elements
		name = new TextField(apiNameLabel);
		apiMode = new ComboBox(apiModeLabel);
        apiMode.setWidth("150px");
		apiMode.addItem(ApiMode.WRITE);
		apiMode.addItem(ApiMode.READ);
		description = new TextField(apiDescriptionLabel);
        description.setWidth("300px");
		hash = null;

		// setup validation
		name.addValidator(new EmptyStringValidator(stringValidationMsg));
		apiMode.addValidator(new ApiKeyModeValidator(apiModeValidationMsg));
		description.addValidator(new EmptyStringValidator(stringValidationMsg));

		// add elements to layout
		addComponent(name);
		addComponent(apiMode);
		addComponent(description);

	}

	private void populateElements(ApiKey apiKey) {

		apiMode.setValue(apiKey.getApiMode());
		name.setValue(apiKey.getName());
		description.setValue(apiKey.getDescription());

		if (apiKey.getHash() != null) {

			if (hash == null) {

				String hashNameLabel = MifssMessages.getMessage("label.api.hash");
				hash = new TextField(hashNameLabel);
                hash.setWidth("300px");
				addComponent(hash);

			}

			hash.setValue(apiKey.getHash());

		}
	}

}
